package com.example.hyznbk.view.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.hyznbk.R;
import com.example.hyznbk.view.adapter.BasePopupMenuAdapter;


/**
 * Created by tangminglong on 16/12/8.
 */

public class PopupMenu extends PopupWindow{
    private final Activity activity;
    private final ListView lv;

    private OnItemClickListener onItemClickListener;

    public PopupMenu(Activity activity,int menuWidth,int backgroundRes){

        super(activity);
        this.activity = activity;
        this.setAnimationStyle(R.style.popupWindowAnimationStyle);
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popView = inflater.inflate(R.layout.listview_popupmenu, null);
        LinearLayout linearLayout = (LinearLayout) popView.findViewById(R.id.linear_popupmenu_bg);

        /*if (backgroundColor != 0)
        {
            linearLayout.setBackgroundColor(backgroundColor);
        }*/
        linearLayout.setBackgroundResource(backgroundRes);
        this.setContentView(popView);
        this.setWidth(
                menuWidth == LinearLayout.LayoutParams.MATCH_PARENT ?
                        LinearLayout.LayoutParams.MATCH_PARENT : dip2px(activity,menuWidth));
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        /*ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);*/

        lv = (ListView) popView.findViewById(R.id.lv_popupmenu);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onItemClickListener != null) {
                    if (lv.getAdapter() instanceof BasePopupMenuAdapter) {
                        ((BasePopupMenuAdapter) lv.getAdapter()).setCheckPosition(position);
                    }
                    onItemClickListener.onItemClick(view, position, id);
                }
                dismiss();
            }
        });
    }




public void setCheckPosition(int position){
    ((BasePopupMenuAdapter) lv.getAdapter()).setCheckPosition(position);
}



    public void setListAdapter(BaseAdapter adapter){
        lv.setAdapter(adapter);
    }


    // dip转换为px
    private int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    // 点击监听接口
    public interface OnItemClickListener {
        void onItemClick(View item, int position, long id);
    }

    // 设置监听
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设置显示的位置
     *
     * @param view
     *            这里的x,y值自己调整可以
     */
    public void showLocation(View view,int x,int y) {
        //setBackgroundAlpha(0.4f);
        showAsDropDown(view, dip2px(activity, x),
                dip2px(activity, y));


    }


    private View view;

    public String getCheckedStr(){
        return ((BasePopupMenuAdapter)lv.getAdapter()).getCheckPositionStr();
    }

    public int getCheckIndex(){
        return ((BasePopupMenuAdapter)lv.getAdapter()).getCheckPositionIndex();
    }
    public void setCheckIndex(int index){
        ((BasePopupMenuAdapter)lv.getAdapter()).setCheckPosition(index);
    }
}
